package com.pomr.model.health;
/**
 * 个人档案
 * @author Administrator
 *
 */

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.Formula;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.pomr.model.menu.HealthCondition;
import com.pomr.model.menu.Industry;
import com.pomr.model.menu.Marriage;
import com.pomr.model.menu.Sex;
import com.pomr.model.sys.Agency;
import com.pomr.model.sys.City;
import com.pomr.model.sys.Coutry;
import com.pomr.model.sys.Province;
import com.pomr.model.sys.User;
@Entity
@Table(name="person_record",uniqueConstraints = {@UniqueConstraint(columnNames={"idcard"})})
public class PersonRecord implements Serializable{
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Province getProvince() {
		return province;
	}
	public void setProvince(Province province) {
		this.province = province;
	}
	public String getProvinceName() {
		return provinceName;
	}
	public void setProvinceName(String provinceName) {
		this.provinceName = provinceName;
	}
	public City getCity() {
		return city;
	}
	public void setCity(City city) {
		this.city = city;
	}
	public String getCityName() {
		return cityName;
	}
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")//AUTO 在mysql中需要指定使用本地策略生成主键值
	private Integer id;//编号
	@Column(name="name",length=50)
	private String name;//姓名
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="sex_id") 
	private Sex sex;//性别 1男2女
	@Formula("(select s.name from menu_sex s where s.id=sex_id)")
	private String sexName;//性别名
	
	@Column(name="birth")
	@JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd")
	private Date birth;//出生日期
	@Column(name="idcard",length=30,nullable=false)
	private String idcard;//身份证号 新生儿使用父母身份证号并且在后跟01.....
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="province_id") 
	private Province province;//省
	@Formula("(select pr.name from province pr where pr.id=province_id)")
	private String provinceName;//省名
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="city_id") 
	private City city;//市
	@Formula("(select ci.name from city ci where ci.id=city_id)")
	private String cityName;//市名
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="coutry_id") 
	private Coutry coutry;//县
	@Formula("(select c.name from coutry c where c.id=coutry_id)")
	private String coutryName;//县名
	
	@Column(name="addr",length=100)
	private String addr;//详细地址
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="industry_id") 
	private Industry industry;//职业
	@Formula("(select i.name from menu_industry i where i.id=industry_id)")
	private String industryName;
	@Column(name="tel",length=20)
	private String tel;//电话
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="marriage_id")
	private Marriage marriage;//婚姻状况
	@Formula("(select m.name from menu_marriage m where m.id=marriage_id)")
	private String marriageName;
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="createuser_id")
	private User user;//创建人
	@Formula("(select u.name from user u where u.id=createuser_id)")
	private String userName;
	@JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
	@Column(name="create_time")
	private Date createTime;//创建时间
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="createagency_id")
	private Agency agency;//所属机构
	@Formula("(select a.name from agency a where a.id=createagency_id)")
	private String agencyName;
	@Column(name="status",length=1,columnDefinition="int default 0")
	private String status;//状态 1：作废
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="healthCondition_id")
	private HealthCondition healthCondition;//健康状态
	@Formula("(select h.name from menu_health_condition h where h.id=healthCondition_id)")
	private String healthConditionName;
	@Column(name="child",length=1,columnDefinition="INT default 0",nullable=false)
	private int child;//1 新生儿
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Sex getSex() {
		return sex;
	}
	public void setSex(Sex sex) {
		this.sex = sex;
	}
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	public String getIdcard() {
		return idcard;
	}
	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}
	public Coutry getCoutry() {
		return coutry;
	}
	public void setCoutry(Coutry coutry) {
		this.coutry = coutry;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public Industry getIndustry() {
		return industry;
	}
	public void setIndustry(Industry industry) {
		this.industry = industry;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public Marriage getMarriage() {
		return marriage;
	}
	public void setMarriage(Marriage marriage) {
		this.marriage = marriage;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public Agency getAgency() {
		return agency;
	}
	public void setAgency(Agency agency) {
		this.agency = agency;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public HealthCondition getHealthCondition() {
		return healthCondition;
	}
	public void setHealthCondition(HealthCondition healthCondition) {
		this.healthCondition = healthCondition;
	}
	public int getChild() {
		return child;
	}
	public void setChild(int child) {
		this.child = child;
	}
	public String getSexName() {
		return sexName;
	}
	public void setSexName(String sexName) {
		this.sexName = sexName;
	}
	public String getCoutryName() {
		return coutryName;
	}
	public void setCoutryName(String coutryName) {
		this.coutryName = coutryName;
	}
	public String getIndustryName() {
		return industryName;
	}
	public void setIndustryName(String industryName) {
		this.industryName = industryName;
	}
	public String getMarriageName() {
		return marriageName;
	}
	public void setMarriageName(String marriageName) {
		this.marriageName = marriageName;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getAgencyName() {
		return agencyName;
	}
	public void setAgencyName(String agencyName) {
		this.agencyName = agencyName;
	}
	public String getHealthConditionName() {
		return healthConditionName;
	}
	public void setHealthConditionName(String healthConditionName) {
		this.healthConditionName = healthConditionName;
	}
	public String getUpdateHql(){
		StringBuilder sb = new StringBuilder();
		if(idcard != null){
			sb.append("idcard ='"+idcard+"'");
		}
		if(name != null){
			sb.append(",name ='"+name+"'");
		}
		if(sex!=null && sex.getId() > 0){
			sb.append(",sex.id ="+sex.getId());
		}
		if(birth != null){
			sb.append(",birth ="+birth);
		}
		if(idcard != null){
			sb.append(",idcard ='"+idcard+"'");
		}
		if(coutry!=null && coutry.getId() != null){
			sb.append(",coutry.id ='"+coutry.getId()+"'");
		}
		if(addr != null){
			sb.append(",addr ='"+addr+"'");
		}
		if(industry!=null && industry.getId() > 0){
			sb.append(",industry.id ="+industry.getId());
		}
		if(tel != null){
			sb.append(",tel ='"+tel+"',");
		}
		if(marriage!=null && marriage.getId() > 0){
			sb.append(",marriage.id ="+marriage.getId());
		}
		if(user != null && user.getId().intValue() > 0){
			sb.append(",user.id ="+user.getId());
		}
		if(createTime != null){
			sb.append(",createTime ="+createTime);
		}
		if(agency!=null && agency.getId().intValue() > 0){
			sb.append(",agency.id ="+agency.getId());
		}
		if(status != null){
			sb.append(",status ='"+status+"'");
		}
		if(healthCondition!=null && healthCondition.getId()>0){
			sb.append(",healthCondition.id ="+healthCondition.getId());
		}
		if(child >= 0){
			sb.append(",child ="+child);
		}
		
		String hql = sb.toString();
		if(hql.startsWith(",")){
			hql = hql.replaceFirst(",", "");
		}
		return sb.toString();
	}
}
